package com.example.db;


import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;

public class DbopenHelper {

	private static final String DATABASE_NAME = "stretchingTime1.db";
	private static final int DATABASE_VERSION = 1;
	public static SQLiteDatabase mDB;
	private DatabaseHelper mDBHelper;
	private Context mCtx;
	private int _id;
	private String name, toTime, fromTime, onoff;
	private int sun, mon, tue, wed, thur, fri, sat;
	private Intent intent;
	private class DatabaseHelper extends SQLiteOpenHelper{

		// 생성자
		public DatabaseHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
		}

		// 최초 DB를 만들때 한번만 호출된다.
		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(Databases.CreateDB._CREATE);

		}

		// 버전이 업데이트 되었을 경우 DB를 다시 만들어 준다.
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS "+Databases.CreateDB._TABLENAME);

			
		}
	}

	public DbopenHelper(Context context){
		this.mCtx = context;
	}

	public DbopenHelper open() throws SQLException{
		mDBHelper = new DatabaseHelper(mCtx, DATABASE_NAME, null, DATABASE_VERSION);
		mDB = mDBHelper.getWritableDatabase();
		return this;
	}

	public void close(){
		mDB.close();
	}

	// Insert DB
	public long insertColumn(String mon, String tue, String wed, String thu, String fri, String sat,
			String sun, int fromtimehour, int fromtimeminute, int totimehour, int totimeminute, String onoff){
		ContentValues values = new ContentValues();
		values.put(Databases.CreateDB.MON, mon);
		values.put(Databases.CreateDB.TUE, tue);
		values.put(Databases.CreateDB.WED, wed);
		values.put(Databases.CreateDB.THU, thu);
		values.put(Databases.CreateDB.FRI, fri);
		values.put(Databases.CreateDB.SAT, sat);
		values.put(Databases.CreateDB.SUN, sun);
		values.put(Databases.CreateDB.FROMTIMEHOUR, fromtimehour);
		values.put(Databases.CreateDB.FROMTIMEMINUTE, fromtimeminute);
		values.put(Databases.CreateDB.TOTIMEHOUR, totimehour);
		values.put(Databases.CreateDB.TOTIMEMINUTE, totimeminute);
		values.put(Databases.CreateDB.ONOFF, onoff);
		

		return mDB.insert(Databases.CreateDB._TABLENAME, null, values);
	}

	// Update DB
	public boolean updateColumn(long id , String mon, String tue, String wed, String thu,
			String fri, String sat, String sun, int fromtimehour, int fromtimeminute,
			int totimehour, int totimeminute, String onoff){
		ContentValues values = new ContentValues();
		values.put(Databases.CreateDB.MON, mon);
		values.put(Databases.CreateDB.TUE, tue);
		values.put(Databases.CreateDB.WED, wed);
		values.put(Databases.CreateDB.THU, thu);
		values.put(Databases.CreateDB.FRI, fri);
		values.put(Databases.CreateDB.SAT, sat);
		values.put(Databases.CreateDB.SUN, sun);
		values.put(Databases.CreateDB.FROMTIMEHOUR, fromtimehour);
		values.put(Databases.CreateDB.FROMTIMEMINUTE, fromtimeminute);
		values.put(Databases.CreateDB.TOTIMEHOUR, totimehour);
		values.put(Databases.CreateDB.TOTIMEMINUTE, totimeminute);
		values.put(Databases.CreateDB.ONOFF, onoff);
		return mDB.update(Databases.CreateDB._TABLENAME, values, "_id="+id, null) > 0;
	}

	// Delete ID
	public boolean deleteColumn(int fromtimehour, int totimehour){
		return mDB.delete(Databases.CreateDB._TABLENAME, "FROMTIMEHOUR="+fromtimehour+ " AND " + "TOTIMEHOUR="+totimehour ,
				null)>=0;

	}
	
	// Delete Contact
	public boolean deleteColumn(String number){
		return mDB.delete(Databases.CreateDB._TABLENAME, "contact="+number, null) > 0;
	}
	
	// Select All
	public static Cursor getAllColumns(){
		return mDB.query(Databases.CreateDB._TABLENAME, null, null, null, null, null, null);
		
	}

	// ID 컬럼 얻어 오기(select)
	public Cursor getColumn(long id , String mon, String tue, String wed, String thu,
			String fri, String sat, String sun, int fromtimehour, int fromtimeminute,
			int totimehour, int totimeminute, String onoff ){

		Cursor c = mDB.query(Databases.CreateDB._TABLENAME, null, 
				"_id="+id, null, null, null, null);
		if(c != null && c.getCount() != 0)
			c.moveToFirst();
		return c;
	}
	public SQLiteDatabase getWritableDatabase() {
		// TODO Auto-generated method stub
		return null;
	}
	
}







